let util = require('util');
let mysqlPool = require('./MySQLPool');

class PayAcctDao {
    constructor(){

    }

    async save(acct) {

        let sql = 'insert into pay_acct (uid, type, acct_no, realname, create_time)' +
            '                    values (?,   ?,    ?,       ?,   now())';
        let args = [acct.uid, acct.type, acct.acct, acct.realName];
        let res = await mysqlPool.query(sql, args);
        if (res){
            return res.insertId;
        }
    
        return 0;
    }

    async update(acct) {
        let sql = 'update pay_acct set acct_no = ?, realname = ? where uid = ? and type = ?';
        let args = [acct.acct, acct.realName, acct.uid, acct.type];
        let res = await mysqlPool.query(sql, args);
        if (res){
            let affected = res.affectedRows;
            return affected;
        }
    
        return 0;
    }

    /**
     * 
     * @param {{uid: String, type: String}} acct 
     */
    async get(acct) {
        let sql = 'select * from pay_acct where uid = ? and type = ? limit 1';
        let args = [acct.uid, acct.type];
        let res = await mysqlPool.query(sql, args);
        if (res){
            return res[0];
        }
    
        return null;
    }
}

let payAcctDao = new PayAcctDao();
module.exports = payAcctDao;